#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Moonflow
{
   /// <summary>
   /// 编辑器界面拓展
   /// </summary>
   public static class MFEditorUI
   {
      public delegate void DrawListItem<T>(T item, int index);
      public static void DivideLine(Color color)
      {
         using (var rect = new EditorGUILayout.HorizontalScope())
         {
            Handles.color = color;
            Handles.DrawLine(new Vector2(rect.rect.x, rect.rect.y),
               new Vector2(rect.rect.x + rect.rect.width, rect.rect.y));
         }
      }
      /// <summary>
      /// 分页列表
      /// </summary>
      /// <param name="objects">被操作列表</param>
      /// <param name="index">当前序号</param>
      /// <param name="foldout">展开状态</param>
      /// <param name="pageNumber">单页显示数量</param>
      /// <typeparam name="T">被操作列表的类型</typeparam>
      public static void DrawFlipList<T>(DrawListItem<T> drawListItemFunc, List<T> objects, ref int index, ref bool foldout, int pageNumber)
      {
         int count = objects?.Count ?? 0;
         using (new EditorGUILayout.HorizontalScope("box"))
         {
            foldout = EditorGUILayout.Foldout(foldout, "Object");
            EditorGUILayout.LabelField($"{index * pageNumber}-{index * pageNumber + pageNumber}/{count}");
            if (GUILayout.Button("<-"))
            {
               index--;
               if (index < 0) index = 0;
            }
            if (GUILayout.Button("->"))
            {
               index++;
               if (index > count / pageNumber) index = count / pageNumber;
            }
         }

         if (foldout && objects != null)
         {
            using (new EditorGUILayout.VerticalScope())
            {
               for (int i = index * pageNumber; i < Mathf.Min(index * pageNumber + pageNumber, objects.Count); i++)
               {
                  drawListItemFunc(objects[i], i);
               }
            }
         }
         index = EditorGUILayout.IntSlider(index, 0, Mathf.Max(1,count) / pageNumber);

      }
      
      /// <summary>
      /// 分页列表
      /// </summary>
      /// <param name="objects">被操作列表</param>
      /// <param name="index">当前序号</param>
      /// <param name="foldout">展开状态</param>
      /// <param name="pageNumber">单页显示数量</param>
      /// <typeparam name="T">被操作列表的类型</typeparam>
      public static void DrawFlipList<T>(DrawListItem<T> drawListItemFunc, List<T> objects, ref int index, int pageNumber)
      {
         bool foldout = true;
         DrawFlipList(drawListItemFunc, objects, ref index, ref foldout, pageNumber);
      }
      
      /// <summary>
      /// 分页列表
      /// </summary>
      /// <param name="objects">被操作列表</param>
      /// <param name="index">当前序号</param>
      /// <param name="foldout">展开状态</param>
      /// <param name="pageNumber">单页显示数量</param>
      /// <typeparam name="T">被操作列表的类型</typeparam>
      public static void DrawFlipList<T>(List<T> objects, ref int index, ref bool foldout, int pageNumber) where T:Object
      {
         int count = objects?.Count ?? 0;
         using (new EditorGUILayout.HorizontalScope("box"))
         {
            foldout = EditorGUILayout.Foldout(foldout, "Object");
            EditorGUILayout.LabelField($"{index * pageNumber}-{index * pageNumber + pageNumber}/{count}");
            if (GUILayout.Button("<-"))
            {
               index--;
               if (index < 0) index = 0;
            }
            if (GUILayout.Button("->"))
            {
               index++;
               if (index > count / pageNumber) index = count / pageNumber;
            }
         }

         if (foldout && objects != null)
         {
            for (int i = index * pageNumber; i < Mathf.Min(index * pageNumber + pageNumber, objects.Count); i++)
            {
               EditorGUILayout.ObjectField($"Target{i}", objects[i], typeof(T), true);
            }
         }
         index = EditorGUILayout.IntSlider(index, 0, Mathf.Max(1,count) / pageNumber);
      }
      
      /// <summary>
      /// 分页列表
      /// </summary>
      /// <param name="objects">被操作列表</param>
      /// <param name="index">当前序号</param>
      /// <param name="pageNumber">单页显示数量</param>
      /// <typeparam name="T">被操作列表的类型</typeparam>
      public static void DrawFlipList<T>(List<T> objects, ref int index, int pageNumber) where T:Object
      {
         bool foldout = true;
         DrawFlipList(objects, ref index, ref foldout, pageNumber);
      }

      public static void InitReorderableList<T>(ReorderableList showList, List<T> list)
      {
         showList = new ReorderableList(list, typeof(T),true, false, true, true);
         showList.drawElementCallback = DrawListItems;
      }
      public static void DrawReorderableList(ReorderableList showList, SerializedObject so)
      {
         
      }

      private static void DrawListItems(Rect rect, int index, bool isActive, bool isFocused)
      {
         
      }

      public static void SaveFile(Object obj, MFEditorDialogContent dialogContent)
      {
         string path = EditorUtility.SaveFilePanelInProject(dialogContent.title, dialogContent.defaultName, dialogContent.extension, dialogContent.message);
         if (path.Length != 0)
         {
            AssetDatabase.CreateAsset(obj, path);
            AssetDatabase.Refresh();
         }
      }
   }
}
#endif